//获取当天的00:00点和23:30点
// export let firstTime = new Date(new Date(new Date().getTime()).setHours(0,0,0,0));
// console.log(firstTime);
// export let lastTime = new Date(new Date(new Date().getTime()).setHours(23,30,0,0));
// console.log(lastTime);



/**
 *  上下两种方法选其一
 */

//根据某天日期获取00:00点和23:30点
export const firstTime1 = (date1) => {
	return new Date(new Date(new Date(date1).getTime()).setHours(0, 0, 0, 0));
}
let firstTime = firstTime1("2021-05-26") //给下面函数用,赋值时变量名跟函数名不能一样

export const lastTime1 = (date1) => {
	return new Date(new Date(new Date(date1).getTime()).setHours(23, 30, 0, 0));
}
let lastTime = lastTime1("2021-05-26") //给下面函数用,赋值时变量名跟函数名不能一样


//获取当前时间点往前每隔半小时的时间,往前多少个,哪个时间开始往前可设置
export function getDateSliteDes(endDate, splitTime, count) {
	if (!endDate) {
		endDate = lastTime;
	}
	if (!splitTime) {
		splitTime = 30 * 60 * 1000;
	}
	if (!count) {
		count = 48;
	}
	let endTime = endDate.getTime();
	let mod = endTime % splitTime;
	if (mod > 0) {
		endTime -= mod;
	}
	let dateArray = [];
	while (count-- > 0) {
		let d = new Date();
		d.setTime(endTime - count * splitTime);
		dateArray.push(d);
	}
	return dateArray;
}
//获取当前时间点往后每隔半小时的时间,往后多少个,哪个时间开始往后可设置
export function getDateSliteIns(startDate, splitTime, count) {
	if (!startDate) {
		startDate = firstTime;
	}
	if (!splitTime) {
		splitTime = 30 * 60 * 1000;
	}
	if (!count) {
		count = 0;
	}
	let startTime = startDate.getTime();
	let mod = startTime % splitTime;
	if (mod > 0) {
		startTime -= mod;
	}
	let dateArray = [];
	while (count < 48) {
		let d = new Date();
		d.setTime(startTime + count * splitTime);
		dateArray.push(d);
		count++
	}
	return dateArray;
}
let des = getDateSliteDes();
let ins = getDateSliteIns();
//格式化unix标准时间 pos：des/ins
export function format(pos, type) {
	let a = pos
	let arr = []
	let str = ''
	let str1 = ''
	for (let i in a) {
		let y = a[i].getFullYear()
		let mm = a[i].getMonth() + 1
		let d = a[i].getDate()
		let h = a[i].getHours()
		let m = a[i].getMinutes()
		h = h < 10 ? '0' + h : h
		mm = mm < 10 ? '0' + mm : mm
		m = m < 10 ? '0' + m : m
		d = d < 10 ? '0' + d : d
		if (type == 'date') {
			str = `${y}-${mm}-${d} ${h}:${m}`
		}
		if (type == 'hour') {
			str = `${h}:${m}`
			str1 = `${y}-${mm}-${d} ${h}:${m}`
		}
		arr.push({
			stamp: str,
			stampAll: str1
		})
	}
	//console.log("format",arr)
	return arr;
}


//获取今天开始的num天日期的数组
export function getSeveralDay(num) {
	let dayArr = [];
	for (let i = 0; i < num; i++) {
		let day = new Date();
		day.setTime(day.getTime() + 24 * 60 * 60 * 1000 * i);
		let s1 = day.getFullYear() + "-" + (day.getMonth() + 1) + "-" + day.getDate();
		dayArr.push(s1);
	}
	console.log(dayArr)
	return dayArr;
}


export const formatTime = (date, type) => {
	//date为new Date()
	//console.log('formatTime',date)
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	let hour = date.getHours();
	let minute = date.getMinutes();
	let second = date.getSeconds();
	month = month < 10 ? "0" + month : month;
	day = day < 10 ? "0" + day : day;
	hour = hour < 10 ? "0" + hour : hour;
	minute = minute < 10 ? "0" + minute : minute;
	second = second < 10 ? "0" + second : second;
	let arr = [
		year + "-" + month + "-" + day,
		year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second,
		year + "年" + month + "月" + day + " " + hour + ":" + minute + ":" + second,
		year + "年" + month + "月" + day + '日',
		hour + ":" + minute + ":" + second,
		hour + ":" + minute,
		month + "月" + day + '日',
		month + "-" + day,
		year + "-" + month
	]
	return arr[type];
}




export function getWeekStr(dateObj) {
	let dict = ["日", "一", "二", "三", "四", "五", "六"]
	return `星期` + dict[dateObj.getDay()]
}


function getDateInfo(date) {
	return {
		year: date.getFullYear(),
		week: date.getDay(),
		month: (date.getMonth() + 1).toString().padStart(2, '0'),
		day: date.getDate().toString().padStart(2, '0'),
		hour: date.getHours().toString().padStart(2, '0'),
		minute: date.getMinutes().toString().padStart(2, '0'),
		second: date.getSeconds().toString().padStart(2, '0'),
	}
}
const weekDict = ["日", "一", "二", "三", "四", "五", "六"]
const formatDict = {
	date: (d) => `${d.year}-${d.month}-${d.day}`,
	time: (d) => `${d.hour}:${d.minute}:${d.second}`,
	hourMinute: (d) => `${d.hour}:${d.minute}`,
	dateTime(d) {
		return formatDict.date(d) + ' ' + formatDict.time(d)
	},
	zhWeek: (d) => '星期' + weekDict[d.week],
	zhMonthDay: (d) => `${d.month}月${d.day}日`,
}

export function getDateStr(dateObj, type) {
	let info = getDateInfo(dateObj)
	let formatFunc = formatDict[type]
	if (!formatFunc) {
		throw new Error('错误的日期格式' + type)
	}
	return formatFunc(info)
}

export function timestamp(dt) {
	//console.log(dt)
	if (typeof(dt) === 'string') {
		dt = dt.replace(/-/g, '/')
	}
	return (new Date(dt)).getTime()
}